package Lee_code;

import java.util.LinkedList;
import java.util.List;

public class Lee_977 {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int left = 0, right = nums.length-1;
        int sign = nums.length-1;
        if(nums.length==1)
        {
            res[0] = nums[0]*nums[0];
            return res;
        }

        while (right > left)
        {
            if( nums[right]*nums[right] > nums[left]*nums[left] )
            {
                res[sign] = nums[right]*nums[right];
                sign--;
                right--;
                if(right==left)
                {
                    res[sign] = nums[right]*nums[right];
                }
            }
            else if( nums[right]*nums[right] == nums[left]*nums[left] )
            {
                res[sign] = nums[right]*nums[right];
                sign--;
                res[sign] = nums[right]*nums[right];
                right--;
                left++;

                sign--;
                if(right==left)
                {
                    res[sign] = nums[right]*nums[right];
                }
            }
            else if( nums[right]*nums[right] < nums[left]*nums[left] )
            {
                res[sign] = nums[left]*nums[left];
                sign--;
                left++;
                if(right==left)
                {
                    res[sign] = nums[right]*nums[right];
                }
            }
        }
        return res;
    }
}
